package com.haochang.chunkvod.view.vod.spinner;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseListView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * @author qintao
 *         created at 2016/7/22 14:10
 *         内容显示区域
 */

public class ContentView extends RelativeLayout
{

	private BaseListView mListView;
	private QuickAdapter quickAdapter = null;
	private List<SpinnerInfo> dataList = new ArrayList<SpinnerInfo>();
	private OnSelectListener mOnSelectListener;

	public ContentView(Context context, List<SpinnerInfo> list)
	{
		super(context);
		dataList.clear();
		if (!CollectionUtils.isEmpty(list))
		{
			dataList.addAll(list);
		}
		init(context);
	}

	private void init(Context context)
	{
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.view_content_spinner, this, true);
		mListView = (BaseListView) findViewById(R.id.listView);
		mListView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, DipPxConversion.dipToPx(context, 40 * dataList.size())));
		quickAdapter = new QuickAdapter<SpinnerInfo>(context, R.layout.item_spinner_list_layout, dataList)
		{
			@Override
			protected void convert(final BaseAdapterHelper helper, final SpinnerInfo item)
			{
				helper.setText(R.id.tv_name, item.getName());
				if (item.isSelected())
				{
					helper.getView(R.id.img_gou).setVisibility(View.VISIBLE);
					helper.setTextColorRes(R.id.tv_name, R.color.color_f04e39);
				} else
				{
					helper.getView(R.id.img_gou).setVisibility(View.GONE);
					helper.setTextColorRes(R.id.tv_name, R.color.color_737373);
				}
				helper.getView().setOnClickListener(new OnBaseClickListener()
				{
					@Override
					public void onBaseClick(View v)
					{
						onRefresh(item);
						notifyDataSetChanged();
						if (mOnSelectListener != null)
						{
							mOnSelectListener.getValue(item.getCode(), item.getName());
						}
					}
				});
			}
		};
		mListView.setAdapter(quickAdapter);
	}

	private void onRefresh(SpinnerInfo info)
	{
		for (SpinnerInfo itemInfo : dataList)
		{
			if (itemInfo.getCode().equals(info.getCode()))
			{
				itemInfo.setSelected(true);
			} else
			{
				itemInfo.setSelected(false);
			}
		}
	}

	public interface OnSelectListener
	{
		public void getValue(String code, String name);
	}

	public void setOnSelectListener(OnSelectListener onSelectListener)
	{
		mOnSelectListener = onSelectListener;
	}
}
